<?php


namespace app\admin\controller\comment;


use app\common\controller\Adminbase;
use think\Db;

class Comment extends Adminbase
{
    //获取商家列表
    //点击商家获取评价列表
    //商家 省 市 区 详细地址 商家名称 联系人 电话 商家logo 评价数量
    public function index()
    {
        if ($this->request->isAjax()) {
            $where[] = ['is_del', '=', 0];
            $where[] = ['status', 'in', [1, 2]];
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['id', '=', $filter['id']];
                }
                if (isset($filter['business_name'])) {
                    $where[] = ['business_name', 'like', '%' . $filter['business_name'] . '%'];
                }
                if (isset($filter['phone'])) {
                    $where[] = ['phone', 'like', '%' . $filter['phone'] . '%'];
                }
                if (isset($filter['name'])) {
                    $where[] = ['name', 'like', '%' . $filter['name'] . '%'];
                }
                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $where[] = ['province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $where[] = ['city', '=', $city['name']];
                }
                if (isset($filter['c_address'])) {
                    $where[] = ['address', 'like', '%' . $filter['c_address'] . '%'];
                }
                if (isset($filter['operate_type'])) {
                    $where[] = ['operate_type', '=', $filter['operate_type']];
                }
                if (isset($filter['operate_cate'])) {
                    $where[] = ['operate_cate', 'like', '%' . $filter['operate_cate'] . '%'];
                }
            }
            $_list = Db::name('business')
                ->where($where)
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            $total = Db::name('business')
                ->where($where)
                ->count();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $_list[$k]['c_address'] = $v['province'] . $v['city'] . $v['area'] . $v['address'];
                //经营品类 多选
                //operate_cate
                $operate_cate_id = explode(',', $v['operate_cate']);
                $operate_cate = Db::name('operate_cate')
                    ->order('sort desc')
                    ->where('is_del', 0)
                    ->where('id', 'in', $operate_cate_id)
                    ->column('name');
                if ($operate_cate) {
                    $operate_cate = implode(' / ', $operate_cate);
                } else {
                    $operate_cate = '未设置';
                }
                $_list[$k]['operate_cate'] = $operate_cate;
                //经营形式 单选
                //operate_type
                $operate_type = Db::name('operate_type')
                    ->where('is_del', 0)
                    ->where('id', $v['operate_type'])
                    ->find();
                if ($operate_type) {
                    $_list[$k]['operate_type'] = $operate_type['name'];
                } else {
                    $_list[$k]['operate_type'] = '未设置';
                }
                //评论数量
                $comment_count= Db::name('comment')->where([
                    ['business_id','=',$v['id']],
                    ['reply_id','=',0],
                    ['is_del','=',0]
                ])->count();
                $_list[$k]['comment_count'] = $comment_count;
            }
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['id']] = $v['name'];
        }
        //经营品类
        $operate_cate = Db::name('operate_cate')->where([
            'status' => 1,
            'is_del' => 0
        ])->order('sort desc')->select();
        foreach ($operate_cate as $k => $v) {
            $operate_cate1[$v['id']] = $v['name'];
        }
        //经营方式
        $operate_type = Db::name('operate_type')->where([
            'status' => 1,
            'is_del' => 0
        ])->order('sort desc')->select();
        foreach ($operate_type as $k => $v) {
            $operate_type1[$v['id']] = $v['name'];
        }
        $this->assign([
            'proviec' => $proviec,
            'operate_cate1' => $operate_cate1,
            'operate_type1' => $operate_type1
        ]);
        return $this->fetch();
    }

    //
    public function commentList()
    {
        //创建时间 商家商品
        $id = $this->request->param('id/d', 0);
        if ($this->request->isAjax()) {
            $where[] = ['co.reply_id', '=', 0];
            $where[] = ['co.is_del', '=', 0];
            $where[] = ['co.business_id', '=', $id];
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['co.id', '=', $filter['id']];
                }
                if (isset($filter['status'])) {
                    $where[] = ['co.status', '=', $filter['status']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['co.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }
                if (isset($filter['goods_name'])) {
                    $where[] = ['g.name', 'like', '%' . $filter['goods_name'] . '%'];
                }
                if (isset($filter['order_no'])) {
                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }
            }
            $_list = Db::name('comment')->alias('co')
                ->join('goods g', 'co.goods_id=g.id')
                ->join('member m', 'co.member_id=m.id')
                ->join('order o', 'co.order_id=o.id')
                ->where($where)
                ->field('co.*,m.nickname,m.mobile,g.name as goods_name,o.order_no')
                ->page($page, $limit)
                ->order('co.create_time desc')
                ->select();
            $total = Db::name('comment')->alias('co')
                ->join('goods g', 'co.goods_id=g.id')
                ->join('member m', 'co.member_id=m.id')
                ->join('order o', 'co.order_id=o.id')
                ->where($where)
                ->count();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        //经营方式
        $operate_type = Db::name('goods')->where([
            'business_id' => $id,
            'is_del' => 0
        ])->order('create_time desc')->select();
        foreach ($operate_type as $k => $v) {
            $goods_list2[$v['id']] = $v['name'];
        }
        $this->assign([
            'business_id' => $id,
            'goods_list' => $goods_list2
        ]);
        return $this->fetch();
    }

    public function replay()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $insert_data = array(
                'content' => $data['reply_content'],
                'create_time' => time(),
                'reply_id' => $data['id'],
                'reply_role'=>3
            );
            $comment_id = Db::name('comment')->insertGetId($insert_data);
            sendMessage($data['member_id'], '资产标注平台回复了你的评价', 4, 1,0);
            if ($comment_id) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $id = $this->request->param('id/d', 0);
        //获取评价消息
        //获取相关图片
        $data = Db::name('comment')
            ->alias('c')
            ->join('goods g','g.id=c.goods_id')
            ->field('c.*,g.name as goods_name,g.img as goods_img')
            ->find($id);
        $data['img_list'] = Db::name('comment_img')
            ->where('comment_id', '=', $id)
            ->select();
        $return=Db::name('comment')->where(['reply_id'=>$data['id'],'reply_role'=>3])->order('create_time desc')->find();
        $return_content=$return['content'];
        $this->assign([
            'data' => $data,
            'return_content'=>$return_content
        ]);
//        $data['replay_list'] = Db::name('comment')->where([
//            'replay_id' => $id,
//            'is_del' => 0,
//        ])->order('create_time desc')->select();
        return $this->fetch();
    }

    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');


        if (Db::name('comment')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }


    public function getc_cate()
    {
        $id = input('id');
        $query = db('area')->where('pid', $id)->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }
}